import { useStore } from 'dva'
import React, { useEffect, useState, useRef } from 'react'
import { setSearchTypes } from '../../../utils/authority'
import styles from './enquiry.less'
import Step1 from './step1'
import Step2 from './step2'
import Step3 from './step3'
import { useDispatch,useSelector } from 'dva'
import close from '../../../assets/icon/close.png'

function Index({ type, exhibitorID, productID, onOver, onClose }) {
  const ref = useRef()
  const dispatch = useDispatch()
  const [stepIndex, setStepIndex] = useState(1)
  const [enquiryInfo, setEnquiryInfo] = useState({})
  // const [userInfo] = useState({})
  const { userInfo } = useSelector(({ index }) => index)
  useEffect(() => {
    console.log(enquiryInfo, 'enquiryInfo')
  }, [enquiryInfo])
  function updateIndex() {
    if (stepIndex < 3) {
      setStepIndex(stepIndex + 1)
    }
  }
  function updataInfo(obj) {
    setEnquiryInfo({ ...enquiryInfo, ...obj })
    updateIndex()
  }
  function onSubmit(obj) {

    console.log(userInfo);
    
    obj.tradingTime = obj.tradingTime + ' 00:00:00' // new Date(obj.tradingTime).getTime()
    // setEnquiryInfo({ ...enquiryInfo, ...obj })
    dispatch({
      type: 'index/apiPostWantbuy',
      payload: {
        ...enquiryInfo,
        ...obj,
        enable: true,
        exhibitId: productID,
        exhibitorId: exhibitorID,
        creatorId: userInfo?.user?.id,
        // user: {id: ""}
      },
      callback: (status) => {
        if (status) {
          setStepIndex(3)
        }
      }
    })

  }
  // function over() {
  //   onOver()
  // }
  return (
    <div className={styles.box}  >
      <img src={close} className={styles.close} onClick={onClose}></img>
      {
        stepIndex == 1 && <Step1 updataInfo={updataInfo} />
      }
      {
        stepIndex == 2 && <Step2 submit={onSubmit} />
      }
      {
        stepIndex == 3 && <Step3 onNext={onOver} />
      }
    </div>
  )
}

export default Index
